#include "OrderServer.h"
#include "CpuAffinity.h"

OrderServer::OrderServer(const char *listen_ip, uint16_t listen_port,
                         const char *target_ip, uint16_t target_port,
                         int recv_cpu_id, int send_cpu_id)
    : listen_ip_(listen_ip), listen_port_(listen_port),
      target_ip_(target_ip), target_port_(target_port),
      recv_cpu_id_(recv_cpu_id), send_cpu_id_(send_cpu_id)
{
    // rateMonitor_ = std::make_unique<RateMonitor>();
    memoryPool_ = std::make_unique<MemoryPool>(sizeof(OrderPacket), 1024);         // 预分配6万条
    ringBuffer_ = std::make_unique<LockFreeRingBuffer<OrderPacket>>(65536); // 环形缓冲区

    receiver_ = std::make_unique<Receiver>(listen_ip_, listen_port_, *ringBuffer_, *memoryPool_, recv_cpu_id_);
    sender_ = std::make_unique<Sender>(target_ip_, target_port_, *ringBuffer_, *memoryPool_, send_cpu_id_);
}

OrderServer::~OrderServer()
{
    stop();
}

void OrderServer::start()
{
    receiver_->start();
    sender_->start();
    // rateMonitor_->start();
}

void OrderServer::stop()
{
    receiver_->stop();
    sender_->stop();
    // rateMonitor_->stop();
}
